package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {

    @Resource
    private IServeItemService serveItemService;

    @Resource
    private IRegionService regionService;
    /**
     * 服务分页查询
     * @param servePageQueryReqDTO 分页参数
     * @return
     */
    @Override
    public PageResult<ServeResDTO> pageList(ServePageQueryReqDTO servePageQueryReqDTO) {
        PageResult<ServeResDTO> serveResDTOPageResult = PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.listPage(servePageQueryReqDTO.getRegionId()));
        return serveResDTOPageResult;
    }

    /**
     * 服务新增
     * @param serveUpsertReqDTOS 服务参数
     */
    @Override
    public void add(List<ServeUpsertReqDTO> serveUpsertReqDTOS) {
        for (ServeUpsertReqDTO serveUpsertReqDTO : serveUpsertReqDTOS) {
            ServeItem serveItem = serveItemService.getById(serveUpsertReqDTO.getServeItemId());
            if (ObjectUtils.isNull(serveItem) || serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()){
                throw new ForbiddenOperationException("该服务项为空不可添加至区域！");
            }
            //判断是否服务项重复
            Integer count = lambdaQuery()
                    .eq(Serve::getRegionId, serveUpsertReqDTO.getRegionId())
                    .eq(Serve::getServeItemId, serveUpsertReqDTO.getServeItemId())
                    .count();
            if (count > 0){
                throw new ForbiddenOperationException(serveItem.getName() + "服务已存在");
            }
            //新增服务
            Serve serve = BeanUtil.toBean(serveUpsertReqDTO, Serve.class);
            Region region = regionService.getById(serveUpsertReqDTO.getRegionId());
            serve.setCityCode(region.getCityCode());
            serve.setPrice(serveItem.getReferencePrice());
            baseMapper.insert(serve);
        }


    }

    /**
     * 修改服务价格
     * @param id 服务id
     * @param price 服务价格
     */
    @Override
    public void update(Long id, BigDecimal price) {
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getPrice, price)
                .update();
        if (!update){
            throw new CommonException("修改服务价格失败");
        }
    }

    /**
     * 服务上架
     * @param id 服务id
     */
    @Override
    public void onSale(Long id) {
        Serve serve = baseMapper.selectById(id);
        //判断服务是否存在
        if (ObjectUtils.isNull(serve)){
            throw new ForbiddenOperationException("该服务不存在");
        }
        // 判断服务当前状态
        if (serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务已经上架");
        }
        // 判断服务型是否启用
        ServeItem serveItem = serveItemService.getById(serve.getServeItemId());
        if (serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务项未启用，不可上架");
        }
        // 上架
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus,FoundationStatusEnum.ENABLE.getStatus())
                .update();
        if (!update){
            throw new CommonException("上架失败！");
        }
    }

    /**
     * 服务下架
     * @param id 服务id
     */
    @Override
    public void delete(Long id) {
        // 确定一个服务
        Serve serve = this.getById(id);
        // 判断服务状态
        if (serve.getSaleStatus() != FoundationStatusEnum.INIT.getStatus()){
            throw new ForbiddenOperationException("服务非草稿状态，不可删除");
        }
        int count = baseMapper.deleteById(id);
        if (count == 0){
            throw new ForbiddenOperationException("删除失败");
        }
    }

    /**
     * 服务下架
     * @param id 服务id
     */
    @Override
    public void offSale(Long id) {
        // 确定服务
        Serve serve = this.getById(id);
        // 判断状态
        if (serve.getSaleStatus() == FoundationStatusEnum.DISABLE.getStatus()){
            throw new ForbiddenOperationException("服务已经下架");
        }
        //修改状态
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus())
                .update();
        if (!update){
            throw new CommonException("修改失败");
        }
    }

    /**
     * 设置热门
     * @param id 服务id
     */
    @Override
    public void onHot(Long id) {
        // 确定一个服务
        Serve serve = baseMapper.selectById(id);
        //判断服务是否是热门
        if (serve.getIsHot() == 1){
            throw new ForbiddenOperationException("服务已经是热门");
        }
        // 修改状态至热门
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, 1)
                .update();
        if (!update){
            throw new CommonException("修改热门失败");
        }
    }

    /**
     * 取消热门
     * @param id 服务id
     */
    @Override
    public void offHot(Long id) {
        // 确定服务
        Serve serve = baseMapper.selectById(id);
        //判断服务是否非热门
        if (serve.getIsHot() == 0){
            throw new ForbiddenOperationException("服务已取消热门，不可再次取消");
        }
        // 修改状态
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, 0)
                .update();
        if (!update){
            throw new CommonException("取消热门失败!");
        }
    }
}
